<template>
  <div>
    <div class="search-container">
      <el-input v-model="search.username" placeholder="操作人" style="width: 200px; margin-right: 10px" />
      <el-input v-model="search.name" placeholder="操作名称" style="width: 200px; margin-right: 10px" />
      <el-input v-model="search.location" placeholder="操作地址" style="width: 200px; margin-right: 10px" />
      <el-button @click="searchMethod">搜索</el-button>
      <el-button @click="resetSearch">重置</el-button>
      <el-button @click="clean()" type="danger">清空日志</el-button>
    </div>

    <el-table :data="tableData" border style="margin-top: 20px">
      <el-table-column prop="name" label="操作名称" />
      <el-table-column prop="ip" label="IP" />
      <el-table-column prop="location" label="地址" />
      <el-table-column prop="username" label="操作人" />
      <el-table-column prop="time" label="操作时间" />
      <el-table-column label="操作" width="200" fixed="right">
        <template #default="{ row }">
          <el-popconfirm
            title="确定要删除该日志吗？"
            confirm-button-text="确定"
            cancel-button-text="取消"
            @confirm="handleDelete(row)">
            <template #reference>
              <el-button type="danger" size="small">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      layout="sizes, prev, pager, next, jumper, total"
      @size-change="findSize"
      @current-change="findPage"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      :page-sizes="[5, 10, 20, 30, 50]"
      style="text-align: center; padding: 20px 0" />
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import {
  cleanLog, deleteLog, getLogList as pageLog
} from '@/api/Log.js'

// 数据
const tableData = ref([])
const total = ref(0)
const pageSize = ref(10)
const currentPage = ref(1)

// 分页方法
const findSize = (size) => {
  pageSize.value = size
  fetch()
}

const findPage = (page) => {
  currentPage.value = page
  fetch()
}

// 获取用户列表
const fetch = async (page = currentPage.value, size = pageSize.value, params = {}) => {
  try {
    const { data } = await pageLog({
      pageNum: page,
      pageSize: size,
      ...params
    })
    tableData.value = data.records
    total.value = data.total
  } catch (error) {
    console.error('获取列表失败:', error)
    ElMessage.error('获取列表失败')
  }
}

// 删除日志
const handleDelete = (row) => {
  deleteLog(row.id).then(() => {
    ElMessage.success('删除成功')
    fetch()
  }).catch(() => {
    ElMessage.error('删除失败')
  })
}

// 清空日志
const clean = () => {
  ElMessageBox.confirm('确定要清空所有日志吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    cleanLog().then(() => {
      ElMessage.success('清空成功')
      fetch()
    }).catch(() => {
      ElMessage.error('清空失败')
    })
  })
}

// 搜索相关
const search = reactive({
  username: '',
  ip: '',
  name: '',
  location: '',
  time: ''
})

const searchMethod = () => {
  const params = {}
  if (search.username) params.username = search.username
  if (search.ip) params.ip = search.ip
  if (search.name) params.name = search.name
  if (search.location) params.location = search.location
  if (search.time) params.time = search.time

  fetch(1, pageSize.value, params)
  currentPage.value = 1
}

const resetSearch = () => {
  search.username = ''
  search.ip = ''
  search.name = ''
  search.location = ''
  search.time = ''
  fetch()
}

// 页面加载时获取数据
onMounted(async () => {
  await fetch()
})
</script>

<style scoped lang="scss">
.search-container {
  display: flex;
  margin-bottom: 20px;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}

h1 {
  margin-bottom: 20px;
  color: #303133;
}

.transition-box {
  transition: all 0.3s ease;
  border: 1px solid #ebeef5;
  border-radius: 4px;
  padding: 20px;
  background-color: #f9f9f9;
  margin-bottom: 20px;
}

.el-form {
  max-width: 600px;
}

.el-pagination {
  margin: 20px 0;
}

.el-table {
  margin-bottom: 20px;
}

.el-tag {
  font-weight: bold;
}

.el-button + .el-button {
  margin-left: 10px;
}

.el-form-item__content {
  display: flex;
  align-items: center;
}

.el-input-number {
  width: 180px;
}
</style>
